package com.lagou.rabbitmq.demo.homework.listener;

import com.lagou.rabbitmq.demo.homework.entity.Order;
import com.lagou.rabbitmq.demo.homework.entity.OrderDetail;
import com.lagou.rabbitmq.demo.homework.service.RedisService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static com.lagou.rabbitmq.demo.homework.config.RabbitConfig.ORDER_QUEUE_NAME;

/**
 * @Author: gule
 * @Date: 2020/10/16
 */
@Component
public class SaveOrderListener {

    @Autowired
    private RedisService redisService;

    @RabbitListener(queues = "order_queue.order", ackMode = "MANUAL")
    public void onMessage(Order order, Channel channel, Message message) throws IOException {
        System.out.println("写数据库");
        redisService.saveOrder(order.getOrderId());

        System.out.println(order.getOrderId() + "\t" + order.getUserId() + "\t" + order.getStatus());
        for (OrderDetail detail : order.getDetails()) {
            System.out.println(
                    detail.getItemId() + "\t"
                    + detail.getItemName() + "\t"
                    + detail.getItemPrice() + "\t"
                    + detail.getNum()
            );
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
